#include "quicklaunch.h"

QuickLaunch::QuickLaunch(QWidget *parent) : QMainWindow(parent), quickLaunchWindow(new Ui::QuickLaunch)
{
    quickLaunchWindow->setupUi(this);

    //Set the Quick Launch Window as a Popup
    Qt::WindowFlags flags;
    QuickLaunch::setWindowFlags(flags = Qt::Popup);

    readQuickLaunchSettings();

    connect(quickLaunchWindow->quickLaunchOne_Button, SIGNAL(clicked()), this, SLOT(favorite1Path()));
    connect(quickLaunchWindow->quickLaunchTwo_Button, SIGNAL(clicked()), this, SLOT(favorite2Path()));
    connect(quickLaunchWindow->quickLaunchThree_Button, SIGNAL(clicked()), this, SLOT(favorite3Path()));
    connect(quickLaunchWindow->quickLaunchFour_Button, SIGNAL(clicked()), this, SLOT(favorite4Path()));
    connect(quickLaunchWindow->quickLaunchFive_Button, SIGNAL(clicked()), this, SLOT(favorite5Path()));
    connect(quickLaunchWindow->quickLaunchSix_Button, SIGNAL(clicked()), this, SLOT(favorite6Path()));
}

void QuickLaunch::readQuickLaunchSettings()
{
    QSettings QuickLaunchSettings("LifeSync", "Project LifeSync");

    //Quick Launch Favorites: Path Load Settings
    checkIfWebsiteFav1 = QuickLaunchSettings.value("favBool1").toBool();
    checkIfWebsiteFav2 = QuickLaunchSettings.value("favBool2").toBool();
    checkIfWebsiteFav3 = QuickLaunchSettings.value("favBool3").toBool();
    checkIfWebsiteFav4 = QuickLaunchSettings.value("favBool4").toBool();
    checkIfWebsiteFav5 = QuickLaunchSettings.value("favBool5").toBool();
    checkIfWebsiteFav6 = QuickLaunchSettings.value("favBool6").toBool();
    fav1 = QuickLaunchSettings.value("favPath1").toString();
    fav2 = QuickLaunchSettings.value("favPath2").toString();
    fav3 = QuickLaunchSettings.value("favPath3").toString();
    fav4 = QuickLaunchSettings.value("favPath4").toString();
    fav5 = QuickLaunchSettings.value("favPath5").toString();
    fav6 = QuickLaunchSettings.value("favPath6").toString();

    //Quick Launch Favorites: Image Load Settings
    quickLaunchWindow->quickLaunchOne_Button->setStyleSheet(QuickLaunchSettings.value("qlOne", "QPushButton{background: url(:/favoritesImages/websites/images/favoritesImages/websites/firefox.png)}").toString());
    quickLaunchWindow->quickLaunchTwo_Button->setStyleSheet(QuickLaunchSettings.value("qlTwo", "QPushButton{background: url(:/favoritesImages/websites/images/favoritesImages/websites/facebook.png)}").toString());
    quickLaunchWindow->quickLaunchThree_Button->setStyleSheet(QuickLaunchSettings.value("qlThree", "QPushButton{background: url(:/favoritesImages/school/images/favoritesImages/school/email.png)}").toString());
    quickLaunchWindow->quickLaunchFour_Button->setStyleSheet(QuickLaunchSettings.value("qlFour", "QPushButton{background: url(:/favoritesImages/school/images/favoritesImages/school/word.png)}").toString());
    quickLaunchWindow->quickLaunchFive_Button->setStyleSheet(QuickLaunchSettings.value("qlFive", "QPushButton{background: url(:/favoritesImages/school/images/favoritesImages/school/powerpoint.png)}").toString());
    quickLaunchWindow->quickLaunchSix_Button->setStyleSheet(QuickLaunchSettings.value("qlSix", "QPushButton{background: url(:/favoritesImages/school/images/favoritesImages/school/excel.png)}").toString());
}

void QuickLaunch::writeQuickLaunchSettings()
{
    QSettings QuickLaunchSettings("LifeSync", "Project LifeSync");

    //Quick Launch Favorites: Path Save Settings
    QuickLaunchSettings.setValue("favBool1", checkIfWebsiteFav1);
    QuickLaunchSettings.setValue("favBool2", checkIfWebsiteFav2);
    QuickLaunchSettings.setValue("favBool3", checkIfWebsiteFav3);
    QuickLaunchSettings.setValue("favBool4", checkIfWebsiteFav4);
    QuickLaunchSettings.setValue("favBool5", checkIfWebsiteFav5);
    QuickLaunchSettings.setValue("favBool6", checkIfWebsiteFav6);
    QuickLaunchSettings.setValue("favPath1", fav1);
    QuickLaunchSettings.setValue("favPath2", fav2);
    QuickLaunchSettings.setValue("favPath3", fav3);
    QuickLaunchSettings.setValue("favPath4", fav4);
    QuickLaunchSettings.setValue("favPath5", fav5);
    QuickLaunchSettings.setValue("favPath6", fav6);

    //Quick Launch Favorites: Image Save Settings
    QuickLaunchSettings.setValue("qlOne", quickLaunchWindow->quickLaunchOne_Button->styleSheet());
    QuickLaunchSettings.setValue("qlTwo", quickLaunchWindow->quickLaunchTwo_Button->styleSheet());
    QuickLaunchSettings.setValue("qlThree", quickLaunchWindow->quickLaunchThree_Button->styleSheet());
    QuickLaunchSettings.setValue("qlFour", quickLaunchWindow->quickLaunchFour_Button->styleSheet());
    QuickLaunchSettings.setValue("qlFive", quickLaunchWindow->quickLaunchFive_Button->styleSheet());
    QuickLaunchSettings.setValue("qlSix", quickLaunchWindow->quickLaunchSix_Button->styleSheet());
}

void QuickLaunch::favorite1Path()
{
    if(checkIfWebsiteFav1 == true)
        QDesktopServices::openUrl(QUrl(fav1));
    else
    {
        QProcess *myProcess = new QProcess();
        myProcess->start(fav1, arguments);

        if (myProcess->error() == QProcess::FailedToStart)
            QMessageBox::critical( 0, tr("Error"), tr("Incorrect Path. Please check the Favorites window to ensure the correct path is provided"), tr("Ok") );
    }
}

void QuickLaunch::favorite2Path()
{
    if(checkIfWebsiteFav2 == true)
        QDesktopServices::openUrl(QUrl(fav2));
    else
    {
        QProcess *myProcess = new QProcess();
        myProcess->start(fav2, arguments);

        if (myProcess->error() == QProcess::FailedToStart)
            QMessageBox::critical( 0, tr("Error"), tr("Incorrect Path. Please check the Favorites window to ensure the correct path is provided"), tr("Ok") );
    }
}

void QuickLaunch::favorite3Path()
{
    if(checkIfWebsiteFav3 == true)
        QDesktopServices::openUrl(QUrl(fav3));
    else
    {
        QProcess *myProcess = new QProcess();
        myProcess->start(fav3, arguments);

        if (myProcess->error() == QProcess::FailedToStart)
            QMessageBox::critical( 0, tr("Error"), tr("Incorrect Path. Please check the Favorites window to ensure the correct path is provided"), tr("Ok") );
    }
}

void QuickLaunch::favorite4Path()
{
    if(checkIfWebsiteFav4 == true)
        QDesktopServices::openUrl(QUrl(fav4));
    else
    {
        QProcess *myProcess = new QProcess();
        myProcess->start(fav4, arguments);

        if (myProcess->error() == QProcess::FailedToStart)
            QMessageBox::critical( 0, tr("Error"), tr("Incorrect Path. Please check the Favorites window to ensure the correct path is provided"), tr("Ok") );
    }
}

void QuickLaunch::favorite5Path()
{
    if(checkIfWebsiteFav5 == true)
        QDesktopServices::openUrl(QUrl(fav5));
    else
    {
        QProcess *myProcess = new QProcess();
        myProcess->start(fav5, arguments);

        if (myProcess->error() == QProcess::FailedToStart)
            QMessageBox::critical( 0, tr("Error"), tr("Incorrect Path. Please check the Favorites window to ensure the correct path is provided"), tr("Ok") );
    }
}

void QuickLaunch::favorite6Path()
{
    if(checkIfWebsiteFav6 == true)
        QDesktopServices::openUrl(QUrl(fav6));
    else
    {
        QProcess *myProcess = new QProcess();
        myProcess->start(fav6, arguments);

        if (myProcess->error() == QProcess::FailedToStart)
            QMessageBox::critical( 0, tr("Error"), tr("Incorrect Path. Please check the Favorites window to ensure the correct path is provided"), tr("Ok") );
    }
}

QuickLaunch::~QuickLaunch()
{
    delete quickLaunchWindow;
}
